<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="租户id" prop="lesseeId">
        <el-input
          v-model="queryParams.lesseeId"
          placeholder="请输入租户id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="应用编号" prop="appCode">
        <el-select v-model="queryParams.appCode" placeholder="请选择应用编号" clearable size="small">
          <el-option
            v-for="dict in appCodeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="消息标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入消息标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['qywx:message:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['qywx:message:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['qywx:message:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['qywx:message:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="messageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="唯一ID" align="center" prop="id" />
      <el-table-column label="租户id" align="center" prop="lesseeId" />
      <el-table-column label="企业应用id" align="center" prop="agentId" />
      <el-table-column label="应用编号" align="center" prop="appCode" :formatter="appCodeFormat" />
      <el-table-column label="发送用户" align="center" prop="toUser" />
      <el-table-column label="发送部门" align="center" prop="toParty" />
      <el-table-column label="发送标签" align="center" prop="toTag" />
      <el-table-column label="消息标题" align="center" prop="title" />
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="消息内容" align="center" prop="content" />
      <el-table-column label="是否发送" align="center" prop="isSend" />
      <el-table-column label="发送时间" align="center" prop="sendDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sendDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="定时发送时间" align="center" prop="regularlyDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.regularlyDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="返回结果" align="center" prop="returnResult" />
      <el-table-column label="小程序跳转" align="center" prop="page" />
      <el-table-column label="小程序appid" align="center" prop="wxAppId" />
      <el-table-column label="小程序通知详细信息" align="center" prop="contentItem" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['qywx:message:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['qywx:message:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改企业微信消息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="是否有效" prop="isActive">
          <el-input v-model="form.isActive" placeholder="请输入是否有效" />
        </el-form-item>
        <el-form-item label="租户id" prop="lesseeId">
          <el-input v-model="form.lesseeId" placeholder="请输入租户id" />
        </el-form-item>
        <el-form-item label="企业应用id" prop="agentId">
          <el-input v-model="form.agentId" placeholder="请输入企业应用id" />
        </el-form-item>
        <el-form-item label="应用编号">
          <el-select v-model="form.appCode" placeholder="请选择应用编号">
            <el-option
              v-for="dict in appCodeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发送用户" prop="toUser">
          <el-input v-model="form.toUser" placeholder="请输入发送用户" />
        </el-form-item>
        <el-form-item label="发送部门" prop="toParty">
          <el-input v-model="form.toParty" placeholder="请输入发送部门" />
        </el-form-item>
        <el-form-item label="发送标签" prop="toTag">
          <el-input v-model="form.toTag" placeholder="请输入发送标签" />
        </el-form-item>
        <el-form-item label="消息类型">
          <el-select v-model="form.messageType" placeholder="请选择消息类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="消息标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入消息标题" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="跳转链接" prop="url">
          <el-input v-model="form.url" placeholder="请输入跳转链接" />
        </el-form-item>
        <el-form-item label="图文消息的图片" prop="picUrl">
          <el-input v-model="form.picUrl" placeholder="请输入图文消息的图片" />
        </el-form-item>
        <el-form-item label="消息内容" prop="content">
          <el-input v-model="form.content" placeholder="请输入消息内容" />
        </el-form-item>
        <el-form-item label="消息状态" prop="sendFlag">
          <el-input v-model="form.sendFlag" placeholder="请输入消息状态" />
        </el-form-item>
        <el-form-item label="是否发送" prop="isSend">
          <el-input v-model="form.isSend" placeholder="请输入是否发送" />
        </el-form-item>
        <el-form-item label="发送时间" prop="sendDate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.sendDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择发送时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="定时发送时间" prop="regularlyDate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.regularlyDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择定时发送时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="返回结果" prop="returnResult">
          <el-input v-model="form.returnResult" placeholder="请输入返回结果" />
        </el-form-item>
        <el-form-item label="事件ID1" prop="eventId1">
          <el-input v-model="form.eventId1" placeholder="请输入事件ID1" />
        </el-form-item>
        <el-form-item label="事件ID2" prop="eventId2">
          <el-input v-model="form.eventId2" placeholder="请输入事件ID2" />
        </el-form-item>
        <el-form-item label="小程序跳转" prop="page">
          <el-input v-model="form.page" placeholder="请输入小程序跳转" />
        </el-form-item>
        <el-form-item label="小程序appid" prop="wxAppId">
          <el-input v-model="form.wxAppId" placeholder="请输入小程序appid" />
        </el-form-item>
        <el-form-item label="是否放大第一条" prop="emphasisFirstItem">
          <el-input v-model="form.emphasisFirstItem" placeholder="请输入是否放大第一条" />
        </el-form-item>
        <el-form-item label="小程序通知详细信息" prop="contentItem">
          <el-input v-model="form.contentItem" placeholder="请输入小程序通知详细信息" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMessage, getMessage, delMessage, addMessage, updateMessage, exportMessage } from "@/api/qywx/message";

export default {
  name: "Message",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 企业微信消息表格数据
      messageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 应用编号字典
      appCodeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        lesseeId: undefined,
        appCode: undefined,
        title: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        isActive: [
          { required: true, message: "是否有效不能为空", trigger: "blur" }
        ],
        lesseeId: [
          { required: true, message: "租户id不能为空", trigger: "blur" }
        ],
        agentId: [
          { required: true, message: "企业应用id不能为空", trigger: "blur" }
        ],
        appCode: [
          { required: true, message: "应用编号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_qy_app_code").then(response => {
      this.appCodeOptions = response.data;
    });
  },
  methods: {
    /** 查询企业微信消息列表 */
    getList() {
      this.loading = true;
      listMessage(this.queryParams).then(response => {
        this.messageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 应用编号字典翻译
    appCodeFormat(row, column) {
      return this.selectDictLabel(this.appCodeOptions, row.appCode);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        createBy: undefined,
        createTime: undefined,
        updateTime: undefined,
        updateBy: undefined,
        isActive: undefined,
        lesseeId: undefined,
        agentId: undefined,
        appCode: undefined,
        toUser: undefined,
        toParty: undefined,
        toTag: undefined,
        messageType: undefined,
        title: undefined,
        description: undefined,
        url: undefined,
        picUrl: undefined,
        content: undefined,
        sendFlag: undefined,
        isSend: undefined,
        sendDate: undefined,
        regularlyDate: undefined,
        returnResult: undefined,
        eventId1: undefined,
        eventId2: undefined,
        page: undefined,
        wxAppId: undefined,
        emphasisFirstItem: undefined,
        contentItem: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加企业微信消息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMessage(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改企业微信消息";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateMessage(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addMessage(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除企业微信消息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMessage(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有企业微信消息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportMessage(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>